package ar.com.sebasoft.rembrandt.fragment;

import java.io.File;
import java.util.Date;
import java.util.List;

import uk.co.senab.photoview.PhotoViewAttacher;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.util.LruCache;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import ar.com.sebasoft.rembrandt.R;
import ar.com.sebasoft.rembrandt.model.Foto;
import ar.com.sebasoft.rembrandt.util.SystemHelper;

import com.actionbarsherlock.app.SherlockFragment;
import com.androidquery.AQuery;
import com.androidquery.callback.AjaxStatus;
import com.androidquery.callback.BitmapAjaxCallback;
import com.androidquery.util.Constants;

/**
 * @author SebaSOFT
 * 
 */
public class FullscreenArrayFragment extends SherlockFragment {
	/**
	 * @author SebaSOFT
	 * 
	 */
	public interface ViewCreatedListener {

		public void onViewCreated(View view, Bundle savedInstanceState);

	}

	public static final String BUNDLE_FECHA = "fecha";

	public static final String BUNDLE_ID = "id";

	public static final String BUNDLE_TITLE = "title";

	public static final String BUNDLE_PATH = "path";

	private static LruCache<Integer, FullscreenArrayFragment> fragmentCache = new LruCache<Integer, FullscreenArrayFragment>(10);

	private Foto foto = null;

	private ViewCreatedListener vListener = null;

	/**
	 * Create a new instance of CountingFragment, providing "num" as an
	 * argument.
	 */
	public static FullscreenArrayFragment newInstance(int pos, List<Foto> items) {

		FullscreenArrayFragment f = fragmentCache.get(pos);
		if (f == null) {
			f = new FullscreenArrayFragment();

			// Supply path as an argument.
			Bundle args = new Bundle();
			args.putString(BUNDLE_PATH, items.get(pos).getPath());
			args.putString(BUNDLE_TITLE, items.get(pos).getTitulo());
			args.putLong(BUNDLE_ID, items.get(pos).getImgID());
			args.putLong(BUNDLE_FECHA, items.get(pos).getFecha().getTime());
			f.setArguments(args);

			fragmentCache.put(pos, f);
		}

		return f;
	}

	/**
	 * When creating, retrieve this instance's number from its arguments.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (this.getArguments() != null) {
			Bundle args = this.getArguments();
			this.foto = new Foto(args.getLong(BUNDLE_ID), args.getString(BUNDLE_PATH), args.getString(BUNDLE_TITLE), new Date(args.getLong(BUNDLE_FECHA)));
		}
	}

	/**
	 * The Fragment's UI is just a simple text view showing its instance number.
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_fullscreen, container, false);

		AQuery aq = new AQuery(v);

		// Nos aseguramos que se muestren los controles

		if (this.foto != null) {
			// "file://" +
			String url = this.foto.getPath();

			aq.id(R.id.fullscreenPhoto).progress(R.id.progress).image(new File(url), false, 0, new BitmapAjaxCallback() {
				@Override
				protected void callback(String url, ImageView iv, Bitmap bm, AjaxStatus status) {
					Log.d(SystemHelper.APP_TAG, "Ajax status:" + status.getMessage() + "\nE:" + status.getError());
					if (bm != null) {
						iv.setImageBitmap(bm);

						PhotoViewAttacher mAttacher = new PhotoViewAttacher(iv);
						mAttacher.setAllowParentInterceptOnEdge(true);
						mAttacher.setScaleType(ScaleType.FIT_CENTER);
						mAttacher.setMaxScale(4);
						mAttacher.setZoomable(true);
					} else {
						AQuery aq2 = new AQuery(iv);
						aq2.image(R.drawable.ic_no_image).animate(Constants.FADE_IN);
					}

				}
			});
			ImageView imgView = aq.id(R.id.fullscreenPhoto).getImageView();
			// Accesibility
			imgView.setContentDescription(this.foto.getTitulo());
		} else {
			SystemHelper.msjShortToast(R.string.err_no_image);
		}

		return v;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		if (this.vListener != null) {
			this.vListener.onViewCreated(view, savedInstanceState);
		}
		super.onViewCreated(view, savedInstanceState);
	}

	/**
	 * @return the foto
	 */
	public final Foto getFoto() {
		return this.foto;
	}

	/**
	 * @param vListener
	 *            the vListener to set
	 */
	public final void setOnViewCreatedListener(ViewCreatedListener vListener) {
		this.vListener = vListener;
	}

	public static void evictCache() {
		fragmentCache.evictAll();
	}

}
